package com.lzx.iteam.base;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.lzx.iteam.util.AllDialogUtil;
import com.lzx.iteam.util.PreferenceUtil;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 03月 24日
 * Time: 下午2:41
 *
 * @QQ : 1234567890
 * **********************************
 */
public abstract class MyBaseActivity extends FragmentActivity implements View.OnClickListener{

    private Class<?> clazz;
    protected Activity mActivity;
    protected PreferenceUtil mPreferenceUtil;
    protected AllDialogUtil mDialogUtil;
    protected Dialog mBaseWaitDialog;

    protected void onCreate(Bundle savedInstanceState, int layoutResID) {
        // TODO 自动生成的方法存根
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(layoutResID);
        clazz = this.getClass();

        /**
         * 上下文
         */
        mActivity = this;
        /**
         * 自定义 SharedPreference
         */
        mPreferenceUtil = PreferenceUtil.getInstance(mActivity);

        /**
         * DialogUtil
         */
        mDialogUtil = AllDialogUtil.getInstance(this);

        /**
         * 初始化布局
         */
        initContentView();

        /**
         * 初始化view
         */
        initView();

        /**
         * 在这个方法初始化一些数据
         */
        initData();

        /**
         * 添加监听事件
         */
        initListener();
    }

    private void initContentView() {
        // TODO 自动生成的方法存根
        // 获取注释
        FindViewById view = clazz.getAnnotation(FindViewById.class);
        // 获取注释的值
        if (view != null) {
            int id = view.id();
            // 获取累的ContentView方法
            try {
                Method method = clazz.getMethod("ContentView", int.class);
                try {
                    method.setAccessible(true);
                    method.invoke(this, id);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
    }

    private void initView() {
        // TODO 自动生成的方法存根
        // 获取类所有的属性对象
        Field[] fields = clazz.getDeclaredFields();
        if (fields != null) {
            for (Field field : fields) {
                // 获取当前属性的对象
                FindViewById viewById = field.getAnnotation(FindViewById.class);
                // 获取注释的值
                if(viewById != null){
                    int id = viewById.id();
                    // 获取类的findViewById的方法
                    try {
                        Method method = clazz.getMethod("findViewById", int.class);
                        try {
                            Object obj = method.invoke(this, id);
                            field.setAccessible(true);
                            // 将设置好的方法放入属性
                            field.set(this, obj);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    }

    public abstract void initData();

    public abstract void initListener();

    /**
     * 显示等待的dialog
     */
    public void waitDlgShow() {
        if (mBaseWaitDialog == null) {
            mBaseWaitDialog = mDialogUtil.waitDialog();
//            mProgressDialog.setCancelable(false);
            if (!mBaseWaitDialog.isShowing()){
                mBaseWaitDialog.show();
            }
        }

    }

    /**
     * 取消等待的dialog
     */
    public void waitDlgDismiss() {
        if (mBaseWaitDialog != null && mBaseWaitDialog.isShowing()) {
            mBaseWaitDialog.dismiss();
            mBaseWaitDialog = null;
        }
    }


    /** 短暂显示Toast提示(来自String) **/
    protected void showShortToast(String text) {
        Toast.makeText(mActivity, text, Toast.LENGTH_SHORT).show();
    }

    /** 长时间显示Toast提示(来自String) **/
    protected void showLongToast(String text) {
        Toast.makeText(mActivity, text, Toast.LENGTH_LONG).show();
    }


    /**
     * 自定义Toast
     * @param toastText
     */
    /*protected void showWarnToast(String toastText) {
        Toast toast = new Toast(mActivity);
        View warnView = LayoutInflater.from(mActivity).inflate(
                R.layout.toast_warn_layout, null);
        TextView text = (TextView) warnView.findViewById(R.id.warning_tv);
        text.setText(toastText);
        toast.setView(warnView);
        toast.setGravity(Gravity.CENTER, 0, 0);// 起点位置为中间
        toast.show();
    }*/

    /**
     * 显示Dialog
     *//*
    protected void showDialog(){
        final Dialog mRecordDialog = new Dialog(mActivity, R.style.Dialogstyle);
        mRecordDialog.setContentView(R.layout.dialog_layout);
        ImageView dialogImg = (ImageView) mRecordDialog
                .findViewById(R.id.record_dialog_img);
        TextView dialogTextView = (TextView) mRecordDialog
                .findViewById(R.id.record_dialog_txt);
        dialogTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mRecordDialog.dismiss();
            }
        });
        mRecordDialog.show();
    }*/


}
